<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
header('Content-type:text/html;charset=utf-8');

class ActiveController extends AdminbaseController{
    
	public function _initialize() {
		parent::_initialize();
	}
	
	// 后台活动列表
	public function activelist(){
		$where=array();
    	$request=I('request.');
    	if(!empty($request['subject'])){
    		$where['subject']=array('like',"%".$request['subject']."%");
    		$this->assign("subject",$request['subject']);
    	}
    	if(!empty($request['status'])){
    		$where['status']=$request['status'];
    		$this->assign("status",$request['status']);
    	}
    	$time = date("Y-m-d H:i:s",time());
    	$start_time=I('request.start_time');
    	if(!empty($start_time)){
    		$where['create_time'][0]=array(
    				array('EGT',$start_time)
    		);
    	}
    	$end_time=I('request.end_time');
    	if(!empty($end_time)){
    		$where['create_time'][1]=array(
    				array('ELT',$end_time)
    		);
    	}
    	$count = M('active')->where($where)->count();
		$size = $page = $this->page($count, 1);
		
		$ads=M('active')->where($where)->order("series ASC,create_time DESC")
		->limit($page->firstRow, $page->listRows)
		->select();
		if(!empty($ads) && is_array($ads)){
			$existdata=true;
		}else{
			$existdata=false;
		}
		$this->assign("page", $page->show('Admin'));
		$this->assign("ads",$ads);
		$this->assign("existdata", $existdata);
		$this->display();
	}
	
	// 活动添加
	public function activeadd(){
		$this->display();
	}
	
	// 活动添加提交
	public function activeadd_post(){
		if(IS_POST){
			if(!empty($_POST['frm']['thumbsimg'])){
    			$_POST['frm']['thumbsimg'] = sp_asset_relative_url($_POST['frm']['thumbsimg']);
    		}
    		if(!empty($_POST['frm']['thumbimg'])){
    			$_POST['frm']['thumbimg'] = sp_asset_relative_url($_POST['frm']['thumbimg']);
    		}
    		/* if(empty($_POST['frm']['subject'])){
    			$this->error("活动标题不能为空！");
    		} */
    		if(!empty($_POST['post']['video'])){
    			$_POST['post']['video'] = sp_get_image_preview_url($_POST['post']['video']);
    			$m = getTime($_POST['post']['video']); 
    		}
    		p($m);
    		$data = I("post.frm");
			$data['UserID'] = get_current_admin_id();
			$data['create_ip'] = get_real_ip();
			if (M('active')->add($data)!==false) {
				$id = M('active')->max("activeid");
				$body = I("post.post");
				$body['body']=htmlspecialchars_decode($body['body']);
				$body['activeid'] = $id;
				M('activeBody')->add($body);
				if(!empty($_POST['frm']['is_enrol'])){
					if(!empty($_POST['frm']['enrol_type'])){
						$arr = explode("\r\n", $_POST['frm']['enrol_type']);
						if(!empty($arr) && is_array($arr)){
							foreach ($arr as $k=>$v){
								$arr1 = explode("|", $v);
								$enrol[$k]['activeid'] = $id;
								$enrol[$k]['enroli_name'] = $arr1['0'];
								$enrol[$k]['enroli_price'] = $arr1['1'];
								$enrol[$k]['status'] = "1";
								$enrol[$k]['UserID'] = get_current_admin_id();
								$enrol[$k]['create_ip'] = get_real_ip();
								$enrol[$k]['create_time'] = date("Y-m-d H:i:s",time());
							}
						}
					}
				}
				M('activeEnrol')->addAll($enrol);
				$this->success("添加成功",U("Active/activelist"));
			} else {
				$this->error("添加失败！");
			}
		}
	}
	
	// 活动编辑
	public function activeedit(){
		$id=I("get.id",0,'intval');
		$active=M('active')
		->table('sp_active a')
		->join("sp_active_body b on a.activeid = b.activeid")
		->field("a.*,b.desc,b.body")
		->where(array("a.activeid"=>$id))
		->find();
		if(!empty($active) && is_array($active)){
			if(!empty($active['active_area'])){
				$arrarea = explode("_", $active['active_area']);
				if(!empty($arrarea) && is_array($arrarea)){
					foreach ($arrarea as $key=>$val){
						$arr = explode(":", $val);
						$active['area'][$arr['0']]['level'] = $key+1;
						$active['area'][$arr['0']]['value'] = $arr['1'];
					}
				}
			}
		}
		$this->assign('active',$active);
		$this->display();
	}
	
	// 活动编辑提交
	public function activeedit_post(){
		if (IS_POST) {
			$id = I('post.id');
			if(!empty($_POST['frm']['thumbsimg'])){
    			$_POST['frm']['thumbsimg'] = sp_asset_relative_url($_POST['frm']['thumbsimg']);
    		}
    		if(!empty($_POST['frm']['thumbimg'])){
    			$_POST['frm']['thumbimg'] = sp_asset_relative_url($_POST['frm']['thumbimg']);
    		}
    		if(empty($_POST['frm']['subject'])){
    			$this->error("活动标题不能为空！");
    		}
    		$data = I("post.frm");
			$data['update_ip'] = get_real_ip();
			$data['update_time'] = date("Y-m-d H:i:s",time());
			if (M('active')->where(array("activeid"=>$id))->save($data)!==false) {
				$body = I("post.post");
				$count = M('activeBody')->where(array("activeid"=>$id))->count();
				if(!empty($count)){
					$body['desc']=htmlspecialchars_decode($body['desc']);
					$body['body']=htmlspecialchars_decode($body['body']);
					M('activeBody')->where(array("activeid"=>$id))->save($body);
				}else{
					$body['desc']=htmlspecialchars_decode($body['desc']);
					$body['body']=htmlspecialchars_decode($body['body']);
					$body['activeid'] = $id;
					M('activeBody')->add($body);
				}
				if(!empty($_POST['frm']['is_enrol'])){
					M('activeEnrol')->where(array("activeid"=>$id))->delete();
					if(!empty($_POST['frm']['enrol_type'])){
						$arr = explode("\n", $_POST['frm']['enrol_type']);
						if(!empty($arr) && is_array($arr)){
							foreach ($arr as $k=>$v){
								$arr1 = explode("|", $v);
								$enrol[$k]['activeid'] = $id;
								$enrol[$k]['enroli_name'] = $arr1['0'];
								$enrol[$k]['enroli_price'] = $arr1['1'];
								$enrol[$k]['status'] = "1";
								$enrol[$k]['UserID'] = get_current_admin_id();
								$enrol[$k]['create_ip'] = get_real_ip();
								$enrol[$k]['create_time'] = date("Y-m-d H:i:s",time());
							}
							M('activeEnrol')->addAll($enrol);
						}
					}
				}
				$this->success("编辑成功",U("Active/activelist"));
			} else {
				$this->error("编辑失败！");
			}
		}
	}
	
	// 活动删除
	public function activedel(){
		$id = I("get.id",0,"intval");
		if (M('active')->where(array("activeid"=>$id))->delete()!==false) {
			M('activeBody')->where(array("activeid"=>$id))->delete();
			M('activeEnrol')->where(array("activeid"=>$id))->delete();
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	
	// 活动显示/隐藏
	public function activetoggle(){
		if(!empty($_POST['ids']) && isset($_GET["display"])){
			$ids = I('post.ids/a');
			if (M('active')->where(array('activeid'=>array('in',$ids)))->save(array('status'=>1))!==false) {
				$this->success("启用成功！");
			} else {
				$this->error("启用失败！");
			}
		}
		
		if(isset($_POST['ids']) && isset($_GET["hide"])){
			$ids = I('post.ids/a');
			if (M('active')->where(array('activeid'=>array('in',$ids)))->save(array('status'=>2))!==false) {
				$this->success("禁用成功！");
			} else {
				$this->error("禁用失败！");
			}
		}
	}
	public function activeorders(){//活动排序
		$status = parent::_listorders(M('active'),"series","activeid");
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}
	public function actenrollist(){//活动项目列表
		$this->display();
	}
	//根据省获取市
	public function sheng(){
		$pid = I("get.parent_id");
		$type = I("get.type");
		$data=M("region")->where("parent_id='$pid' and region_type='$type'")->field("region_id,parent_id,region_name,region_type")->select();
		exit(json_encode($data));
	}
}